import { TaskTrack } from "./TaskTrack";
import { TaskRule } from "../../../rules/TaskRule";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { TaskInfo } from "../../../model/TaskInfo";
import { ObjectPool } from "../../../../../com/gengine/utils/pools/ObjectPool";
import { TrackItem } from "./item/TrackItem";
import { ETaskType } from "../../../../../Message/Game/ETaskType";
import { Global } from "../../../../../com/gengine/global/Global";
import { ETaskStatus } from "../../../../../Message/Game/ETaskStatus";
import { DataEvent } from "../../../events/DataEvent";
//class TaskTrackCurrent
    
    export  class TaskTrackCurrent extends TaskTrack
    {
        constructor()
        {
            super();
            this._sortRuleCompleted = TaskRule.getTaskGroupSortNormalRule();
            this._sortRuleNotCompleted = TaskRule.getTaskGroupSortNotCompletedRule();
            Dispatcher.addEventListener(EventName.TrackInfoShowOrHide, this.onTrackInfoShowOrHideHandler);
            return;
        }

        public /* override */ addTask(arg1: TaskInfo): void
        {
            var loc1=ObjectPool.getObject(TrackItem);
            loc1.initTask(arg1);
            _trackBox.addItem(loc1);
            if (arg1.sTask.type == ETaskType._ETaskTypeCrossMassacreActivity) 
            {
                this.lineBoxItem = loc1;
            }
            Global.instance.callLater(this.addLater);
            return;
        }

        protected /* override */ addLater(): void
        {
            super.addLater();
            if (this.lineBoxItem) 
            {
                this.lineBoxItem.addLineBox();
                this.lineBoxItem = null;
            }
            return;
        }

        protected /* override */ sortLater(): void
        {
            var loc3=null;
            var loc4=0;
            var loc5=null;
            var loc6=false;
            var loc1=0;
            var loc2=(_trackBox.itemNum - 1);
            while (loc1 < loc2) 
            {
                loc3 = _trackBox.getItemAt(loc1) as TrackItem;
                loc4 = loc1 + 1;
                while (loc4 < loc2 + 1) 
                {
                    loc6 = false;
                    loc5 = _trackBox.getItemAt(loc4) as TrackItem;
                    if (loc3.taskInfo.status < loc5.taskInfo.status) 
                    {
                        _trackBox.swapItem(loc3, loc5);
                        loc6 = true;
                    }
                    else if (loc3.taskInfo.status == loc5.taskInfo.status && loc5.taskInfo.status == ETaskStatus._ETaskStatusHadCompleted) 
                    {
                        if (this._sortRuleCompleted.indexOf(loc3.taskInfo.sTask.group) > this._sortRuleCompleted.indexOf(loc5.taskInfo.sTask.group)) 
                        {
                            _trackBox.swapItem(loc3, loc5);
                            loc6 = true;
                        }
                    }
                    else if (loc3.taskInfo.status == loc5.taskInfo.status && loc5.taskInfo.status == ETaskStatus._ETaskStatusNotCompleted) 
                    {
                        if (this._sortRuleNotCompleted.indexOf(loc3.taskInfo.sTask.group) > this._sortRuleNotCompleted.indexOf(loc5.taskInfo.sTask.group)) 
                        {
                            _trackBox.swapItem(loc3, loc5);
                            loc6 = true;
                        }
                    }
                    if (loc6) 
                    {
                        break;
                    }
                    ++loc4;
                }
                if (loc6) 
                {
                    loc1 = 0;
                    continue;
                }
                ++loc1;
            }
            _trackBox.layChildChanged = true;
            _trackBox.validateNow();
            return;
        }

        /* internal  */onTrackInfoShowOrHideHandler(arg1: DataEvent): void
        {
            var loc2=false;
            var loc3=0;
            var loc4=0;
            var loc1=arg1.data as TrackItem;
            if (loc1 == null) 
            {
                loc2 = Boolean(arg1.data);
                loc4 = _trackBox.itemNum;
                while (loc3 < loc4) 
                {
                    loc1 = _trackBox.getItemAt(loc3) as TrackItem;
                    if (loc2) 
                    {
                        loc1.openInfo();
                        showItemGuide(loc1);
                    }
                    else 
                    {
                        loc1.closeInfo();
                    }
                    ++loc3;
                }
                if (!loc2) 
                {
                    guideHide();
                }
            }
            else if (loc1.isClose) 
            {
                loc1.openInfo();
                showItemGuide(loc1);
            }
            else 
            {
                loc1.closeInfo();
                showItemGuide(loc1);
            }
            _trackBox.layChildChanged = true;
            _trackBox.validateNow();
            _scrollPane.update();
            return;
        }

        protected /* var */_sortRuleCompleted: Array<any>;

        protected /* var */_sortRuleNotCompleted: Array<any>;

        /* internal */ /* var */lineBoxItem: TrackItem;
    }
